import 'package:ycbl/config/pref_keys.dart';
import 'package:ycbl/model/user.dart';

import 'PrefUtil.dart';


/// 用户相关的配置单独存储管理
class UserConfig {

  factory UserConfig() => _getInstance();

  static UserConfig get instance => _getInstance();

  static UserConfig _instance;

  UserConfig._internal() {
    // 初始化的内容
  }

  ///单例
  static UserConfig _getInstance() {
    if(_instance == null) {
      _instance = new UserConfig._internal();
    }
    return _instance;
  }

  /// 保存 User 数据， 如果 user 为 null， 则会清除该数据
  saveUser(User user) {
    //要加 ?，为 null 时清除数据
    saveUserId(user?.stuId);
    saveUserName(user?.stuName);
    saveNickName(user?.nickName);
    saveAvatar(user?.stuImg);
    saveSchool(user?.school);
    saveGrade(user?.grade);
    saveSex(user?.sex);
    saveProvince(user?.province);
    saveProvinceID(user?.provinceID);
    saveGradeID(user?.gradeID);
    saveCity(user?.city);
    saveCityID(user?.cityID);
    saveStuPhone(user?.stuPhone);
  }

  /// 组装 User
  User getUser() {
    var userId = getUserId();
    if (userId == null) {
      return null;
    }

    return User(
      stuId: userId,
      stuName: getUserName(),
      nickName: getNickName(),
      stuImg: getAvatar(),
      school: getSchool(),
      grade: getGrade(),
      sex: getSex(),
        province:getProvince(),
        provinceID:getProvinceID(),
       gradeID:getGradeID(),
      city:getCity(),
        cityID:getCityID(),
        stuPhone :getStuPhone()
    );

  }

  ///------ 用户 id ---------------------------------
  saveUserId(int userId) {
    PrefUtil.saveInt(PrefKeys.k_user_id, userId);
  }
  int getUserId(){
    return PrefUtil.getInt(PrefKeys.k_user_id);
  }

  ///------ 用户名（真实姓名） -----------------------
  saveUserName(String userName) {
    PrefUtil.saveString(PrefKeys.k_user_name, userName);
  }
  String getUserName(){
    return PrefUtil.getString(PrefKeys.k_user_name);
  }

  ///------ 昵称 ---------------------------------
  saveNickName(String nickName) {
    PrefUtil.saveString(PrefKeys.k_nick_name, nickName);
  }
  String getNickName(){
    return PrefUtil.getString(PrefKeys.k_nick_name);
  }

  ///------ 头像 ---------------------------------
  saveAvatar(String avatar) {
    PrefUtil.saveString(PrefKeys.k_user_avatar, avatar);
  }
  String getAvatar(){
    return PrefUtil.getString(PrefKeys.k_user_avatar);
  }

  ///------ 年级 ---------------------------------
  saveGrade(String grade) {
    PrefUtil.saveString(PrefKeys.k_user_grade, grade);
  }
  String getGrade(){
    return PrefUtil.getString(PrefKeys.k_user_grade);
  }

  ///------ 学校 ---------------------------------
  saveSchool(String school) {
    PrefUtil.saveString(PrefKeys.k_user_school, school);
  }
  String getSchool(){
    return PrefUtil.getString(PrefKeys.k_user_school);
  }

  ///------ 性别 ---------------------------------
  saveSex(String sex) {
    PrefUtil.saveString(PrefKeys.k_user_sex, sex);
  }
  String getSex(){
    return PrefUtil.getString(PrefKeys.k_user_sex);
  }

   saveProvince(String province) {
     PrefUtil.saveString(PrefKeys.k_user_province, province);
   }
  String getProvince(){
    return PrefUtil.getString(PrefKeys.k_user_province);
  }

  saveProvinceID(String provinceID) {
    return PrefUtil.saveString(PrefKeys.k_user_provinceID,provinceID);
  }
  String getProvinceID(){
    return PrefUtil.getString(PrefKeys.k_user_provinceID);
  }
   saveGradeID(String gradeID) {
     return PrefUtil.saveString(PrefKeys.k_user_gradeID,gradeID);
   }
  String getGradeID(){
    return PrefUtil.getString(PrefKeys.k_user_gradeID);
  }

   saveCityID(String cityID) {
     return PrefUtil.saveString(PrefKeys.k_user_cityID,cityID);
   }
  String getCityID(){
    return PrefUtil.getString(PrefKeys.k_user_cityID);
  }

   saveCity(String city) {
     return PrefUtil.saveString(PrefKeys.k_user_city,city);
   }
  String getCity(){
    return PrefUtil.getString(PrefKeys.k_user_city);
  }

   saveStuPhone(String stuPhone) {
     return PrefUtil.saveString(PrefKeys.k_user_stuPhone,stuPhone);
   }

  String getStuPhone(){
    return PrefUtil.getString(PrefKeys.k_user_stuPhone);
  }
}